package com.ubing.boot.notesforjava.test.juc;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : volatile_test_without
 * @Description : volatile_test_without
 * @Author : Xxxxx
 * @Date: 2023-03-14 10:23
 */
public class volatile_test_without {
    List lists=new ArrayList();
    //    volatile List lists= Collections.synchronizedList(new ArrayList());
    public void add(Object o){
        lists.add(o);
    }
    public int size(){
        return lists.size();
    }

    public static void main(String[] args) {
        volatile_test_without volatile_test_without = new volatile_test_without();
        final Object lock=new Object();


        new Thread(()->{
            synchronized (lock){
                System.out.println("t2 start");
                if(volatile_test_without.size()!=5){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("t2 over");
                    lock.notify();
                }
            }
        }).start();


        new Thread(()->{
            synchronized (lock){
                for (int i = 0; i <10 ; i++) {
                    volatile_test_without.add(new Object());
                    System.out.println("add"+ i);
                    if(volatile_test_without.size()==5){
                        lock.notify();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }).start();
    }
}
